package com.chen.service.Impl;

import com.chen.exception.BusinessException;
import com.chen.mapper.DeptMapper;
import com.chen.mapper.EmpMapper;
import com.chen.pojo.Dept;
import com.chen.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Override
    public List<Dept> findall() {
        return deptMapper.findall();
    }

    //根据id删除部门
    @Override
    public void deleteById(Integer id) {
        //判断如果该部门下有员工，则不能删除
        Integer count = empMapper.countEmpByDeptId(id);
        if(count>0){
            throw new BusinessException("该部门下有员工，不能删除");
        }
            deptMapper.deleteById(id);
    }

    @Override
    public void addDept(Dept dept) {
        //补全基础属性 ctreate_time update_time
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.addDept(dept);
    }

    @Override
    public Dept getInfo(Integer id) {
        Dept dept =deptMapper.getById(id);
        return dept;
    }

    @Override
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDept(dept);
    }
}
